/******************************************************************************

 ******************************************************************************
  File Name	:recorder.h
  Version		:001
  Author		:liangH
  Description	:define recorder class for recording
  History		:20120522 new
  Modification	:20121029 liangH add index file of recorder

******************************************************************************/
#ifndef __RECORDER_H__
#define __RECORDER_H__

#include "config.h"
//#include "config.h"

/*20121029 liangH add index file of recorder*/
#define  INDEX_HEADER_SIGNATURE	0x12345678/*305419896*/
#define INDEX_ITEM_SIGNATURE		0x87654321/*2271560481*/

#define INDEX_CREATE		0
#define INDEX_APPEND		1
#define INDEX_READWRITE	2
#define INDEX_READONLY		3

/*the head struct of index file*/
typedef struct tag_LISTFILE_FILEHEADER
{
	unsigned long m_signature;/*the flag of this struct, INDEX_HEADER_SIGNATURE*/
	//char	m_verDev[28];/*device version, that is the same with machine type*/
	char	m_verFile;/*the version of recorder file, different device has version information of itself*/
	char	m_begYear;/*the start time of creating file*/
	char	m_begMonth;
	char	m_begDay;
	char	m_begHour;
	char	m_begMinute;
	char	m_begSecond;
	char	m_endYear;/*the end time of creating file*/
	char	m_endMonth;
	char	m_endDay;
	char	m_endHour;
	char	m_endMinute;
	char	m_endsScond;
	char reserve;
	unsigned short  m_total_files;/*the total number of recorder file*/
	unsigned short  m_cur_file;/*the current position of deleting fle*/
	unsigned short  m_devType;/*device type of digital format*/
	//char	m_szdevType[12];/*device type of string format*/
	//char m_reserved[36];
}INDEX_FILEHEADER;

/*the struct of every index item*/
typedef struct tag_INDEX_ITEM
{
	unsigned long m_signature;/*the flag of this struct, INDEX_ITEM_SIGNATURE*/
	unsigned long m_file_size;/*file size*/
	
	unsigned char	m_state;/*the state that if file is deleted. 0x5a shows file is deleted, 0x01 shows curent file and other shows file exits. the initialization value is 0*/
	unsigned char	m_year;
	unsigned char m_month;
	unsigned char m_mday;

	int m_stime_hms;/*the start time of file,  =h*3600+m*60+s*/
	int m_etime_hms;/*the end time of file,  =h*3600+m*60+s*/
	
	unsigned char m_record_type;/*recorder type, 0 shows normal recorder 1 shows handle recorder and 2 shows alarm recorder*/

	char	m_dir[32];/*file directory*/
	char	m_name[TOTAL_NAME_LEN + 1];/*file name that dose not contain path*/
	char reserve[4];
}INDEX_ITEM;
/*20121029 liangH add end index file of recorder*/

class Recorder{
	/*variable*/
	private: 
		unsigned char recorder_flag[4];/*recorder flag shows that if recorder is needed, 0 shows stop recorder and 1 shows start recorder*/
		unsigned char recorder_mode[4];/*recorder mode. 1 shows auto recorder, 2 shows manual recorder, 3 shows alarm recorder and 4 shows timer recorder*/
		unsigned char recorder_audio_composite_switch[4];/*recorder audio composite switch. 1 shows add audio and 0 shows no audio*/
		unsigned char recorder_distinguishability[4];/*recorder distinguishability*/
		unsigned char recorder_quality[4];/*recorder quality, 0 - 255 level*/
		unsigned int recorder_code_stream[4];/*recorder code stream, 0 shows main streaming and 1 shows sub streaming*/
		unsigned char recorder_frame_rate[4];/*recorder frame rate*/
		unsigned char recorder_I_frame_interval[4];/*the interval of I frame*/
		unsigned char recorder_code_stream_flag[4];/*code stream flag, 0 shows dynamic streaming and 1 shows static streaming*/
		char name[4][100];/*recorder name of four channel*/
		/*the flag of search condition. bit0 shows search by channel ID, bit 1 shows search by start time, bit 2 shows search by end time,
		bit3 shows search by recorder type and bit 4 shows search by recorder source*/
		unsigned char search_flag;/*the flag of searching recorder*/
		unsigned char search_recorder_channelID;/*the channelID of recorder file to be searched*/
		unsigned char search_start_time[14];/*the start time of recorder file to be searched. the format is YYYYMMDDHHMMSS: 20121201112035*/
		unsigned char search_end_time[14];/*the end time of recorder file to be searched. the format is YYYYMMDDHHMMSS: 20121202112500*/
		unsigned char search_recorder_type;/*the type of recorder file to be searched*/
		unsigned char search_recorder_source;/*the source of recorder file to be searched*/
		char search_file_name[47];/*the file name of searching result, that contains clientID of PC, mac address and time*/
		pthread_mutex_t recordthread_lock;/*the thread lock of recorder*/
		pthread_mutex_t recordthread_control_lock;/*the thread control lock of recorder*/
		pthread_mutex_t read_write_lock;/*read and write lock*/
		pthread_t recorder_thread_write;/*write thread of channel 0*/
		pthread_t recorder_thread_write1;/*write thread of channel 1*/
		pthread_t recorder_thread_write2;/*write thread of channel 2*/
		pthread_t recorder_thread_write3;/*write thread of channel 3*/
		unsigned int recorder_port[4];/*port of four channel*/
		SOCKET socketID[4];/*socket ID of four channel*/
		struct sockaddr_in	receive_addr[4];
		int file_pointer[4];/*file pointer of four channel*/
		char time_control[4];/*time control flag of recorder, four channel(-1 not mount sd card, 0 is init , 1 is record)*/
		unsigned char time_interval[4];/*time interval of recorder, four channel(the interval is 5 minutes, 15 minutes, 30 minutes)*/
		HI_BOOL file_close_flag[4];/*the flag of closing file*/
		int file_error_pointer;/*the point of error file*/
		unsigned char current_partion;/*the current partion in which save recorder file*/
		unsigned char recorder_addr[4];/*the address of recorder, 1 shows sd and 2 shows hdd*/
		unsigned char cover_strategy[4];/*the strategy of recorder covers, 1 shows auto cover and 2 manual cover*/
		unsigned char alarm_cover_strategy[4];/*the strategy of alarm recorder covers, 1 shows auto cover and 2 manual cover*/
		unsigned char stop_flag[4];/*the flag of stopping recorder, 0 shows normal stopping and 1 shows abnormal stopping*/
		char send_recorder_file[128];/*the file name of sending recorder file in recorder_thread_send pthread*/
		char send_recorder_list[128];/*the file name of sending recorder list in recorder_list_thread_send pthread*/
		unsigned char recorder_list_sts;/*the status of sending recorder list thread*/
		unsigned char recorder_file_sts;/*the status of sending recorder file thread*/
		//it must add alarm class object
		//alarm: ALARM

	/*constructed function and deconstructed function*/
	public:
		Recorder();/*constructed function*/
		~Recorder();/*deconstructed function*/

	public:
	/*set,get member functin*/
		void set_recorder_flag(unsigned char channelID, unsigned char flag);
		unsigned char get_recorder_flag(unsigned char channelID);
		void set_recorder_mode(unsigned char channelID, unsigned char temp_mode);
		unsigned char get_recorder_mode(unsigned char channelID);
		void set_recorder_audio_composite_switch(unsigned char channelID, unsigned char temp_recorder_audio_composite_switch);
		unsigned char get_recorder_audio_composite_switch(unsigned char channelID);
		void set_recorder_distinguishability(unsigned char channelID, unsigned char distinguishability);
		unsigned char get_recorder_distinguishability(unsigned char channelID);
		void set_recorder_quality(unsigned char channelID, unsigned char quality);
		unsigned char get_recorder_quality(unsigned char channelID);
		void set_recorder_code_stream(unsigned char channelID, unsigned char stream);
		unsigned int get_recorder_code_stream(unsigned char channelID);
		unsigned char get_recorder_code_stream_pc(unsigned char channelID);
		void set_recorder_frame_rate(unsigned char channelID, unsigned char rate);
		unsigned char get_recorder_frame_rate(unsigned char channelID);
		unsigned char get_recorder_frame_rate_pc(unsigned char channelID);
		void set_recorder_I_frame_interval(unsigned char channelID, unsigned char interval);
		unsigned char get_recorder_I_frame_interval(unsigned char channelID);
		void set_recorder_code_stream_flag(unsigned char channelID, unsigned char flag);
		unsigned char get_recorder_code_stream_flag(unsigned char channelID);
		void set_recorder_addr(unsigned char channelID, unsigned char temp_recorder_addr);
		unsigned char get_recorder_addr(unsigned char channelID);
		void set_cover_strategy(unsigned char channelID, unsigned char temp_cover_strategy);
		unsigned char get_cover_strategy(unsigned char channelID);
		void set_alarm_cover_strategy(unsigned char channelID, unsigned char temp_alarm_cover_strategy);
		unsigned char get_alarm_cover_strategy(unsigned char channelID);
		void set_stop_flag(unsigned char channelID, unsigned char temp_stop_flag);
		unsigned char get_stop_flag(unsigned char channelID);
		HI_BOOL get_file_close_flag(unsigned char channelID);
		void set_file_close_flag(unsigned char channelID, HI_BOOL temp_file_close_flag);
		void set_recorder_name(unsigned char channelID,unsigned char *src_name);
		void get_recorder_name(unsigned char channelID, char *dest_name);
		void set_recorder_port(unsigned char channelID, unsigned int temp_port);
		unsigned int get_recorder_port(unsigned char channelID);
		SOCKET get_socketID(unsigned char channelID);
		void set_time_control(unsigned char channelID, char temp_time_control);
		char get_time_control(unsigned char channelID);
		void set_time_interval(unsigned char channelID, char temp_time_interval);
		char get_time_interval(unsigned char channelID);
		char get_time_interval_pc(unsigned char channelID);
		int get_file_pointer(unsigned char channelID);
		void set_file_pointer(unsigned char channelID, int temp_file_pointer);
		void set_current_partion(unsigned char temp_current_partion);
		unsigned char get_current_partion();
		void set_search_flag(unsigned char temp_search_flag);
		unsigned char get_search_flag();
		void set_search_recorder_channelID(unsigned char temp_recorder_channleID);
		unsigned char get_search_recorder_channelID();
		void set_search_start_time(unsigned char * temp_start_time);
		void get_search_start_time(unsigned char * temp_start_time);
		void set_search_end_time(unsigned char * temp_end_time);
		void get_search_end_time(unsigned char * temp_end_time);
		void set_search_recorder_type(unsigned char temp_recorder_type);
		unsigned char get_search_recorder_type();
		void set_search_recorder_source(unsigned char temp_recorder_source);
		unsigned char get_search_recorder_source();
		void set_search_file_name(char * temp_search_file_name);
		void get_search_file_name(char * temp_search_file_name);
		pthread_t get_recorder_thread_write();
		void set_recorder_thread_write(pthread_t thread_ID);
		pthread_t get_recorder_thread_write1();
		void set_recorder_thread_write1(pthread_t thread_ID);
		pthread_t get_recorder_thread_write2();
		void set_recorder_thread_write2(pthread_t thread_ID);
		pthread_t get_recorder_thread_write3();
		void set_recorder_thread_write3(pthread_t thread_ID);
		void set_send_recorder_file(char * temp_send_recorder_file);
		void get_send_recorder_file(char * temp_send_recorder_file);
		void clear_send_recorder_file();
		void set_send_recorder_list(char * temp_send_recorder_list);
		void get_send_recorder_list(char * temp_send_recorder_list);
		void clear_send_recorder_list();
		void set_recorder_list_sts(unsigned char tmp_recorder_list_sts);
		unsigned char get_recorder_list_sts();
		void set_recorder_file_sts(unsigned char tmp_recorder_file_sts);
		unsigned char get_recorder_file_sts();
	/*set,get member functin end*/
	
	public:
	/*basic function*/
		unsigned char create_recorder_thread(unsigned char recorder_type);/*create recorder thread*/
		unsigned char recover_recorder_thread();/*after format success, recover recorder thread*/
		unsigned char stop_recorder_thread(unsigned char stop_flag);/*stop recorder thread*/
		unsigned char create_recorder_thread_alone(unsigned char channelID, unsigned char recorder_type, unsigned char switch_flag);/*create recorder thread of one channel*/
		unsigned char stop_recorder_thread_alone(unsigned char channelID, unsigned char stop_flag);/*stop recorder thread of one channel*/
		void initialize_socket();/*initialize socket*/
		void close_socket();/*close socket*/
		void open_file(char *file_name, int name_len, unsigned char channelID, unsigned char recorder_type);/*open recorder file*/
		int modify_index_item(char *dir_path, char *filename);/*modify index file*/
		int modify_index_head(char *dir_path, char mode);/*modify index head by mode*/
		int delete_index_item(char *dir_path, char *filename);/*delete index file*/
		char get_channelnum(char *filename);/*get channel number by file name*/
		int create_index_file(char *dir_path);/*create index file*/
		int delete_index_file(char *dir_path);/*delete index file*/
		int read_index_file(char *dir_path);/*read index file*/
		int search_index_file(char *dir_path);/*search index file and create result file*/
		int search_index_file_again(char *dir_path);/*search index file and create result file again*/
		int get_recorder_file(char *dir_path, char * file_name);/*search index file by filename, then send this file to FTP*/
		int close_recorder_file();/*close the recorder file*/
		int close_recorder_file_alone(unsigned char channelID);/*close the recorder file by channelID*/
		int whether_index_exist(char *dir_path);/*check if index file exists*/
		FILE *open_index_file(char *dir_path, int flag);/*open index file*/
		int write_index_file_head(FILE *pFile);/*write head information of index file*/
		int get_dir_time(char *dir_name, unsigned char *year, unsigned char *month, unsigned char *day);/*get year, month and day from directory name*/
	/*basic function end*/
	
	private:	
	/*inside process function*/
		/*20121029 liangH add index file of recorder*/
		int get_record_type(char *file_name, unsigned char *recorder_type);/*get recorder type from recorder name*/
		char get_channel_id(char *file_name);/*get channel ID from recorder name*/
		int get_start_time(char *file_name);/*get start time from recorder name*/
		int add_one_index_item(char *filepath, unsigned char channelID, unsigned char recorder_type, unsigned char recorder_source, struct tm *file_tm);/*add one index item to index file*/
		/*20121029 liangH add end index file of recorder*/
	/*inside process function end*/
	
};

#endif

